﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    /// 管理后台中间下边控件。
    /// </summary>
    /// <remarks>管理后台中间下边控件。</remarks>
    [ToolboxItemFilter("GSC.WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundCenterEndControl runat=server height=10px width=100%></{0}:BackgroundCenterEndControl>")]
    [SRDescription("Manage_CenterEnd", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundCenterEndControlDesigner))]
    public class BackgroundCenterEndControl : CompositeControl, INamingContainer, ICompositeControlDesignerAccessor
    {
        #region ICompositeControlDesignerAccessor
        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        #endregion
        private TableCell _leftCell;
        private TableCell _centerCell;
        private TableCell _rightCell;
        private HtmlGenericControl _tbody;
        TableRow _row;
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>GuoShuCan.SDK.Web.TemplatePanelOwner</value>
        [Browsable(false)]
        public TableCell LeftCell
        {
            get
            {
                return _leftCell;
            }
        }
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>TableCell</value>
        [Browsable(false)]
        public TableCell CenterCell
        {
            get
            {
                return _centerCell;
            }
        }
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>TableCell</value>
        [Browsable(false)]
        public TableCell RightCell
        {
            get
            {
                return _rightCell;
            }
        }
        /// <summary>
        /// 获取或设置左边背景图片。
        /// </summary>
        /// <remarks>获取或设置左边背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterEnd_LeftCellBackImage", typeof(Properties.Resources))]
        public string LeftCellBackImage
        {
            get
            {
                object o = this.ViewState["LeftCellBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["LeftCellBackImage"] = value;
            }
        }
        /// <summary>
        /// </summary>
        /// <remarks></remarks>
        /// <value>System.Web.UI.HtmlControls.HtmlGenericControl</value>
        [Browsable(false)]
        public System.Web.UI.HtmlControls.HtmlGenericControl Tbody
        {
            get
            {
                return _tbody;
            }
        }
        /// <summary>
        /// 获取行对象。
        /// </summary>
        /// <remarks>获取行对象。</remarks>
        /// <value>System.Web.UI.WebControls.TableRow</value>
        [Browsable(false)]
        public TableRow Row
        {
            get
            {
                return _row;
            }
        }
        /// <summary>
        /// 获取或设置中间背景图片。
        /// </summary>
        /// <remarks>获取或设置中间背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterEnd_CenterCellBackImage", typeof(Properties.Resources))]
        public string CenterCellBackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }
        /// <summary>
        /// 获取或设置右边背景图片。
        /// </summary>
        /// <remarks>获取或设置右边背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterEnd_RightCellBackImage", typeof(Properties.Resources))]
        public string RightCellBackImage
        {
            get
            {
                object o = this.ViewState["RightCellBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["RightCellBackImage"] = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                base.OnPreRender(e);
            }
            //
            this.Attributes["cellPadding"] = "0";
            this.Attributes["cellSpacing"] = "0";
            this.Attributes["rules"] = "none";
            this.Style["table-layout"] = "fixed";
            this.Style["overflow"] = "hidden";
            //
            this.LeftCell.Height = this.Height;
            this.LeftCell.Width = this.LeftCellWidth;
            this.LeftCell.Style["overflow"] = "hidden";
            this.LeftCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.LeftCellBackImage))
                this.LeftCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Left.gif")) + ")";
            else
                this.LeftCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.LeftCellBackImage) + ")";
            //
            this.CenterCell.Height = this.Height;
            if (this.CenterCellWidth.IsEmpty)
                this.CenterCell.Style["width"] = "auto";
            else
                this.CenterCell.Width = this.CenterCellWidth;
            this.CenterCell.Style["overflow"] = "hidden";
            this.CenterCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.CenterCellBackImage))
                this.CenterCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Center.gif")) + ")";
            else
                this.CenterCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.CenterCellBackImage) + ")";
            //
            this.RightCell.Height = this.Height;
            this.RightCell.Width = this.RightCellWidth;
            this.RightCell.Style["overflow"] = "hidden";
            this.RightCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.RightCellBackImage))
                this.RightCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Right.gif")) + ")";
            else
                this.RightCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.RightCellBackImage) + ")";
        }
        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                this.OnPreRender(EventArgs.Empty);
            }
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }

        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            this._tbody = new HtmlGenericControl("tbody");
            this._row = new TableRow();
            this._row.ID = "Row";
            this._tbody.Controls.Add(this._row);
            //
            this._leftCell = new TableCell();
            this._leftCell.ID = "Left";
            //
            this._centerCell = new TableCell();
            this._centerCell.ID = "Center";
            //
            this._rightCell = new TableCell();
            this._rightCell.ID = "Right";
            //
            this._row.Cells.Add(this._leftCell);
            this._row.Cells.Add(this._centerCell);
            this._row.Cells.Add(this._rightCell);
            //
            this.Controls.Add(this._tbody);
        }

        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        public virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundCenterEndControl), "GSC.WebControls.BackgroundCenterEndControl.Images." + resourceUrl);
        }

        /// <summary>
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }

        /// <summary>
        /// 获取或设置里左边例宽度。
        /// </summary>
        /// <remarks>获取或设置里左边例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [Bindable(true)]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        [DefaultValue(typeof(Unit), "160px")]
        [Category("Layout")]
        [SRDescription("Manage_CenterEnd_LeftCellWidth", typeof(Properties.Resources))]
        public Unit LeftCellWidth
        {
            get
            {
                object o = this.ViewState["LeftCellWidth"];
                if (o == null) return Unit.Pixel(160);
                return (Unit)o;
            }
            set
            {
                this.ViewState["LeftCellWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置里右边例宽度。
        /// </summary>
        /// <remarks>获取或设置里右边例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue(typeof(Unit), "10px")]
        [Category("Layout")]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        [SRDescription("Manage_CenterEnd_RightCellWidth", typeof(Properties.Resources))]
        public Unit RightCellWidth
        {
            get
            {
                object o = this.ViewState["RightCellWidth"];
                if (o == null) return Unit.Pixel(10);
                return (Unit)o;
            }
            set
            {
                this.ViewState["RightCellWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置里中间例宽度。
        /// </summary>
        /// <remarks>获取或设置里中间例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [DefaultValue(typeof(Unit), "Empty")]
        [Category("Layout")]
        [Bindable(true)]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        [SRDescription("Manage_CenterEnd_CenterCellWidth", typeof(Properties.Resources))]
        public Unit CenterCellWidth
        {
            get
            {
                object o = this.ViewState["CenterCellWidth"];
                if (o == null) return Unit.Empty;
                return (Unit)o;
            }
            set
            {
                this.ViewState["CenterCellWidth"] = value;
            }
        }

        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "table";
            }
        }

        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;
            }
        }
    }
}
